#include "resource.h"
#include "addlinedlg.h"
#include "project_p.h"
#include "projectcfgdialog.h"

#include <iostream>

static std::string CString2StdString( const CString& cstring )
{
    CT2CA pszConvertedAnsiString( cstring );
    std::string stdstr( pszConvertedAnsiString );
    return stdstr;
}

static CString StdString2CString( const std::string& stdstring )
{
    CString cstring = stdstring.c_str();
    return cstring;
}

ProjectCfgDialog::ProjectCfgDialog( CWnd* pParent )
    : CDialog( IDD_PROJECTCFG_DIALOG, pParent )
{
}

ProjectCfgDialog::~ProjectCfgDialog()
{
    std::cerr << "~project dialog\n";
}

void ProjectCfgDialog::DoDataExchange( CDataExchange* pDX )
{
    CDialog::DoDataExchange( pDX );
    DDX_Control( pDX, IDOK, ok );
    DDX_Control( pDX, IDCANCEL, cancel );
    DDX_Control( pDX, IDC_EDIT_BASEDIR, edit_basedir );
    DDX_Control( pDX, IDC_LIST_SRC, list_src );
    DDX_Control( pDX, IDC_BUTTON_ADDSRC, src_add );
    DDX_Control( pDX, IDC_BUTTON_REMOVESRC, src_remove );
    DDX_Control( pDX, IDC_LIST_INCLUDE, list_include );
    DDX_Control( pDX, IDC_BUTTON_ADDINCLUDE, include_add );
    DDX_Control( pDX, IDC_BUTTON_REMOVEINCLUDE, include_remove );
    DDX_Control( pDX, IDC_LIST_CFLAG, list_cflag );
    DDX_Control( pDX, IDC_BUTTON_ADDCFLAG, cflag_add );
    DDX_Control( pDX, IDC_BUTTON_REMOVECFLAG, cflag_remove );
    DDX_Control( pDX, IDC_LIST_LDFLAG, list_ldflag );
    DDX_Control( pDX, IDC_BUTTON_ADDLDFLAG, ldflag_add );
    DDX_Control( pDX, IDC_BUTTON_REMOVELDFLAG, ldflag_remove );
}

BEGIN_MESSAGE_MAP( ProjectCfgDialog, CDialog )
    ON_BN_CLICKED( IDOK, OnBnClickedOK )
    ON_BN_CLICKED( IDCANCEL, OnBnClickedCancel )
    ON_BN_CLICKED( IDC_BUTTON_ADDSRC, OnBnClickedAddSrc )
    ON_BN_CLICKED( IDC_BUTTON_REMOVESRC, OnBnClickedRemoveSrc )
    ON_BN_CLICKED( IDC_BUTTON_ADDINCLUDE, OnBnClickedAddInclude )
    ON_BN_CLICKED( IDC_BUTTON_REMOVEINCLUDE, OnBnClickedRemoveInclude )
    ON_BN_CLICKED( IDC_BUTTON_ADDCFLAG, OnBnClickedAddCFlag )
    ON_BN_CLICKED( IDC_BUTTON_REMOVECFLAG, OnBnClickedRemoveCFlag )
    ON_BN_CLICKED( IDC_BUTTON_ADDLDFLAG, OnBnClickedAddLDFlag )
    ON_BN_CLICKED( IDC_BUTTON_REMOVELDFLAG, OnBnClickedRemoveLDFlag )
END_MESSAGE_MAP()

BOOL ProjectCfgDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    /// load project settings
    ProjectPrivate* d = ProjectPrivate::self();
//     std::cerr << d << '\n';
    std::cerr << d->project_name << '\n';

    edit_basedir.SetWindowText( StdString2CString( d->project_basedir ) );

    ProjectTarget target = d->project_targets[0];

    std::vector<std::string>::const_iterator it = target.target_files.begin();
    std::vector<std::string>::const_iterator end = target.target_files.end();
    while ( it != end ) {
        std::cerr << *it << '\n';
        list_src.AddString( StdString2CString( *it ) );
        ++it;
    }

    std::vector<std::string>::const_iterator it2 = target.project_includedirs.begin();
    std::vector<std::string>::const_iterator end2 = target.project_includedirs.end();
    while ( it2 != end2 ) {
        list_include.AddString( StdString2CString( *it2 ) );
        ++it2;
    }

    std::vector<std::string>::const_iterator it4 = d->optflags.defined_marcos.begin();
    std::vector<std::string>::const_iterator end4 = d->optflags.defined_marcos.end();
    while ( it4 != end4 ) {
        list_cflag.AddString( StdString2CString( *it4 ) );
        ++it4;
    }

    std::vector<std::string>::const_iterator it3 = target.project_linktargets.begin();
    std::vector<std::string>::const_iterator end3 = target.project_linktargets.end();
    while ( it3 != end3 ) {
        list_ldflag.AddString( StdString2CString( *it3 ) );
        ++it3;
    }

    return TRUE;
}

void ProjectCfgDialog::OnBnClickedOK()
{
    /// save project settings
    ProjectPrivate* d = ProjectPrivate::self();
    CString str;
    edit_basedir.GetWindowText( str );
    d->project_basedir = CString2StdString( str );

    ///     CString str2;
    d->project_targets[0].target_files.clear();
    for ( int i=0; i < list_src.GetCount(); i++ ) {
        list_src.GetText( i, str );
        d->project_targets[0].target_files.push_back( CString2StdString( str ) );
        std::cerr << CString2StdString(str) << '\n';
    }
    d->project_targets[0].project_includedirs.clear();
    for ( int i=0; i < list_include.GetCount(); i++ ) {
        list_include.GetText( i, str );
        d->project_targets[0].project_includedirs.push_back( CString2StdString( str ) );
        std::cerr << CString2StdString(str) << '\n';
    }
    d->project_targets[0].project_linktargets.clear();
    for ( int i=0; i < list_ldflag.GetCount(); i++ ) {
        list_ldflag.GetText( i, str );
        d->project_targets[0].project_linktargets.push_back( CString2StdString( str ) );
        std::cerr << CString2StdString(str) << '\n';
    }
    d->optflags.defined_marcos.clear();
    for ( int i=0; i < list_cflag.GetCount(); i++ ) {
        list_cflag.GetText( i, str );
        d->optflags.defined_marcos.push_back( CString2StdString( str ) );
        std::cerr << CString2StdString(str) << '\n';
    }
    OnOK();
}

void ProjectCfgDialog::OnBnClickedCancel()
{
    /// discard setting changes
    OnCancel();
}

void ProjectCfgDialog::OnBnClickedAddSrc()
{
    /// add a filepath
    AddLineDlg dlgFile;
    dlgFile.DoModal();
    CString filepath = dlgFile.GetStr();
    list_src.AddString( filepath );
}

void ProjectCfgDialog::OnBnClickedRemoveSrc()
{
    /// remove the first selected filepath
    for ( int i=0; i < list_src.GetCount(); i++ ) {
        if ( list_src.GetSel( i ) > 0 ) {
            list_src.DeleteString( i );
            break;
        }
    }
}

void ProjectCfgDialog::OnBnClickedAddInclude()
{
    /// add an include
    AddLineDlg dlg;
    dlg.DoModal();
    CString str = dlg.GetStr();
    list_include.AddString( str );
}

void ProjectCfgDialog::OnBnClickedRemoveInclude()
{
    /// remove the first selected include
    for ( int i=0; i < list_include.GetCount(); i++ ) {
        if ( list_include.GetSel( i ) > 0 ) {
            list_include.DeleteString( i );
            break;
        }
    }
}

void ProjectCfgDialog::OnBnClickedAddCFlag()
{
    /// add a cflag
    AddLineDlg dlg;
    dlg.DoModal();
    CString str = dlg.GetStr();
    list_cflag.AddString( str );
}

void ProjectCfgDialog::OnBnClickedRemoveCFlag()
{
    /// remove the first selected cflag
    for ( int i=0; i < list_cflag.GetCount(); i++ ) {
        if ( list_cflag.GetSel( i ) > 0 ) {
            list_cflag.DeleteString( i );
            break;
        }
    }
}

void ProjectCfgDialog::OnBnClickedAddLDFlag()
{
    /// add a ldflag
    AddLineDlg dlg;
    dlg.DoModal();
    CString str = dlg.GetStr();
    list_ldflag.AddString( str );
}

void ProjectCfgDialog::OnBnClickedRemoveLDFlag()
{
    /// remove the first selected ldflag
    for ( int i=0; i < list_ldflag.GetCount(); i++ ) {
        if ( list_ldflag.GetSel( i ) > 0 ) {
            list_ldflag.DeleteString( i );
            break;
        }
    }
}
